package com.itcs.se.security.tran;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.itcs.box.BoxConstants;
import com.itcs.box.util.LogUtils;
import com.itcs.eureka.service.PayApiFeignClientService;
import com.itcs.se.security.data.req.BoxEncryptReq;
import com.itcs.se.security.data.res.BoxEncryptRes;
import com.itcs.se.security.service.SeSecurityService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @ClassName: TranEncrypt
 * @Description:TODO(这里用一句话描述这个类的作用)
 * @author: CHEN-HF
 * @date: 2018年10月20日 下午9:38:30
 * 
 * @Copyright: 2018 Inc. All rights reserved. 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目
 */
@Component
public class TranEncrypt implements PayApiFeignClientService {

	private Log logger = LogFactory.getLog(getClass());

	@Autowired
	private SeSecurityService seSecurityService;

	@Override
	public String getTrancode() {
		return "encrypt";
	}

	@Override
	@RequestMapping(value = "/encrypt", method = RequestMethod.POST, produces = "application/json")
	public Map<String, Object> exchange(String serverId, String trancode,
										@RequestParam Map<String, Object> reqRecord) {

		logger.info(LogUtils.build("encrypt", "工作密匙加密", "上传数据:[" + JSON.toJSONString(reqRecord) + "]"));
		
		BoxEncryptRes response = new BoxEncryptRes();

		Map<String, Object> res = new HashMap<>();
		try {
			BoxEncryptReq boxRequest = (BoxEncryptReq) reqRecord.get(BoxConstants.BOX_REQ_KEY);
			response = this.handle(boxRequest, response); // 业务处理
		} catch (Exception e) {
			response.setErrCode("9"); // 其他异常或数据库异常
		}
		res.put(BoxConstants.BOX_RES_KEY, response);

		return res;
	}

	public BoxEncryptRes handle(BoxEncryptReq request, BoxEncryptRes response) {

		BoxEncryptRes res = seSecurityService.doEncryptWithSessionKey(request);

		return res;
	}

}
